# _*_ coding : utf-8 _*_
# @Time : 2024-06-04 22:49
# @Author : haowen
# @File : serveApi
# @Project : pyserver
import json
from pydantic import BaseModel
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
import akshare as ak
import pandas as pd

import handle.getshars
import handle.sharesChats
import handle.forecastshars
import handle.getoneshares


app = FastAPI()

# 添加中间件以允许所有地址访问
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 定义/getsharschats接收的数据结构
class SharesData(BaseModel):
    startData: str
    sharesCode: str

# 带模型的
class SharesDataModel(BaseModel):
    startData: str
    sharesCode: str
    sharesModel: str

@app.get("/getshars")
async def getshars():
    # 解决接口访问报错的bug

    data_objects = handle.getshars.getshars()
    # json_data = json.dumps(data_objects,ensure_ascii=False)
    # result = []
    # result.append(data_objects[0])
    # for index in range(1200):
    #     result.append(data_objects[index])

    # print(type(data_objects))
    return {"data": data_objects}

@app.post("/getsharschats")
async def getsharschats(data: SharesData):
    startData = data.startData
    sharesCode = data.sharesCode
    handle.sharesChats.createChars(startData,sharesCode)
    # print(startData)
    # print(sharesCode)
    return {"msg": "股票分析图生成成功!"}

@app.post("/getsharsforecast")
async def getsharsforecast(data: SharesDataModel):
    startData = data.startData
    sharesCode = data.sharesCode
    sharesModel = data.sharesModel
    handle.forecastshars.yuce(startData,sharesCode,sharesModel)
    # print(startData)
    # print(sharesCode)
    return {"msg": "预测成功!"}

@app.post("/getoneshares")
async def getoneshares(data: SharesData):
    # 显示历史所有数据 startData不参与参数列表
    startData = data.startData
    sharesCode = data.sharesCode
    handle.getoneshares.createChars(startData,sharesCode)
    # print(startData)
    # print(sharesCode)
    return {"msg": "单股票分析图生成成功!"}

if __name__ == "__main__":
    uvicorn.run(app, host="127.0.0.1", port=8000)
